(function() {
  var check_retrefno, express, iced, moment, pgsql, query_info, router, uuid, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  express = require('express');

  pgsql = require('pg');

  uuid = require('uuid');

  moment = require('moment');

  router = express.Router();

  module.exports = router;

  router.all('/', function(req, res, next) {
    var client, done, err, result, where_clause, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    where_clause = "(\n    case\n    when t1.chnl in (1,4) then\n        json_extract_path_text(t1.i_pkt, '42') like $1 or\n        json_extract_path_text(t1.i_pkt, '41') like $1 or\n        json_extract_path_text(t1.i_pkt, '4')  like $1 or\n        json_extract_path_text(t1.i_pkt, '11') like $1\n    end or\n    case\n    when t2.chnl = 3 then\n        json_extract_path_text(t2.i_pkt, 'merId') like $1 or\n        json_extract_path_text(t2.i_pkt, 'orderId') like $1\n    end\n)";
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/t40_recvr.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 28
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/t40_recvr.coffee"
          });
          client.query("select count(*)::int as count from pay_t40_recvr as r left join pay_trlist as t1 on t1.uuid = r.tr_uuid_1 and t1.incoming = true left join pay_trlist as t2 on t2.uuid = r.tr_uuid_2 and t2.incoming = false where " + where_clause + " and r.ctime::date = CURRENT_DATE and r.checked = false", ['%' + res.locals.keyword + '%'], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 40
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          res.locals.total = result.rows[0].count;
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/t40_recvr.coffee"
            });
            client.query("select r.*, t1.sys_time as tr1_systime, case when t1.chnl in (1,4) then array[ json_extract_path_text(t1.i_pkt, '42'), json_extract_path_text(t1.i_pkt, '41'), json_extract_path_text(t1.i_pkt, '4'), json_extract_path_text(t1.i_pkt, '11')] end as tr1_info, case when t2.chnl = 3 then array[ json_extract_path_text(t2.i_pkt, 'merId'), json_extract_path_text(t2.i_pkt, 'orderId'), json_extract_path_text(t2.i_pkt, 'txnAmt')] end as tr2_info, t2.n_pkt is not null as tr2_confirm, t2.notes as tr2_notes from pay_t40_recvr as r left join pay_trlist as t1 on t1.uuid = r.tr_uuid_1 and t1.incoming = true left join pay_trlist as t2 on t2.uuid = r.tr_uuid_2 and t2.incoming = false where " + where_clause + " and r.ctime::date = CURRENT_DATE and r.checked = false order by r.serial offset $2 limit $3", ['%' + res.locals.keyword + '%', res.locals.offset, res.locals.page_rows], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 73
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            done();
            res.locals.records = result.rows;
            return res.render('t40_recvr/index');
          });
        });
      };
    })(this));
  });

  query_info = function(req, res, next) {
    var client, done, err, record, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/t40_recvr.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 81
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/t40_recvr.coffee"
          });
          client.query("select r.*, t1.sys_time as tr1_systime, json_extract_path_text(t1.merch, 'name') as tr1_mername, case when t1.chnl in (1,4) then array[ json_extract_path_text(t1.i_pkt, '42'), json_extract_path_text(t1.i_pkt, '41'), json_extract_path_text(t1.i_pkt, '4'), json_extract_path_text(t1.i_pkt, '11')] end as tr1_info, case r.product when 'p1' then o1.p1_fee when 'p2' then o2.p2_fee end as fee, case r.product when 'p1' then ( select array[ pay_chnl::text, pay_merid, acct_no, acct_name, acct_cred_type::text, acct_cred_no ] from pay_p1 as p1 where p1.uuid = o1.p1_uuid ) when 'p2' then ( select array[ pay_chnl::text, pay_merid, acct_no, acct_name, acct_cred_type::text, acct_cred_no, p2_fund, fee_take_type::text, fund_lack_proc::text, p2f.overdraw::text ] from pay_p2 as p2 left join pay_p2_fund as p2f on p2f.uuid = p2.p2_fund where p2.uuid = o2.p2_uuid ) end as order_conf, u.realname as input_user from pay_t40_recvr as r left join pay_trlist as t1 on t1.uuid = r.tr_uuid_1 and t1.incoming = true left join pay_p1_order as o1 on o1.uuid = r.order_uuid left join pay_p2_order as o2 on o2.uuid = r.order_uuid left join web_users as u on u.uuid = r.inputer where r.uuid = $1 and r.ctime::date = CURRENT_DATE and r.checked = false and r.done = false", [req.params.id], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 127
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          if (result.rows.length === 0) {
            done();
            return next(new Error('没有查询到追付记录，请检查.'));
          }
          record = result.rows[0];
          req.body.pay_amt = parseFloat(record.tr1_info[2]) / 100;
          if (record.product === 'p1') {
            req.body.pay_amt = req.body.pay_amt - record.fee;
          }
          req.body.pay_amt = Math.round(req.body.pay_amt * 100) / 100;
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/t40_recvr.coffee"
            });
            client.query("select * from pay_merch where uuid = $1", [record.order_conf[1]], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 149
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            record.pay_merch = result.rows[0];
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/t40_recvr.coffee"
              });
              client.query("select * from pay_p2_fund where uuid = $1", [record.order_conf[6]], __iced_deferrals.defer({
                assign_fn: (function() {
                  return function() {
                    err = arguments[0];
                    return result = arguments[1];
                  };
                })(),
                lineno: 156
              }));
              __iced_deferrals._fulfill();
            })(function() {
              if (err) {
                done(client);
                return next(err);
              }
              record.p2_fund = result.rows[0];
              done();
              res.locals.record = record;
              return next();
            });
          });
        });
      };
    })(this));
  };

  check_retrefno = function(req, res, next) {
    var client, done, err, k, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    _ref = ['uuid', 't40_amt', 'pay_chnl', 'pay_merid', 'acct_no', 'retrefno'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/t40_recvr.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 169
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/t40_recvr.coffee"
          });
          client.query("select case when t1.chnl in (1,4) then json_extract_path_text(t1.o_pkt, '37') end as retrefno from pay_t40_recvr as r left join pay_trlist as t1 on t1.uuid = r.tr_uuid_1 and t1.incoming = true where r.uuid = $1", [req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 183
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          if (result.rows.length === 0) {
            return next(new Error("未查询到追付记录[" + req.body.uuid + "]."));
          }
          if (result.rows[0].retrefno !== req.body.retrefno) {
            return next(new Error('原交易检索参考号不匹配，不能代付'));
          }
          return next();
        });
      };
    })(this));
  };

  router.get('/new/:id', query_info, function(req, res, next) {
    res.locals.breadcrumbs.push({
      'title': '请求追付'
    });
    if (res.locals.record.inputed) {
      return next(new Error('该记录已被追付，不允许重复追付'));
    }
    res.locals.record.t40_amt = req.body.pay_amt;
    return res.render('t40_recvr/new');
  });

  router.post('/new', check_retrefno, function(req, res, next) {
    var client, done, err, overdraw, result, t40_amt, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/t40_recvr.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 207
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        t40_amt = parseFloat(req.body.t40_amt.replace(/,/g, '')) || -1;
        if (t40_amt <= 0.0) {
          return next(new Error('追付金额必须为正数.'));
        }
        if (req.body.fund_overdraw) {
          overdraw = parseFloat(req.body.fund_overdraw.replace(/,/g, '')) || 0.0;
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/t40_recvr.coffee"
          });
          client.query("update pay_t40_recvr set t40_amt = $1, pay_chnl = $2, pay_merid = $3, acct_no = $4, acct_name = $5, acct_cred_type = $6, acct_cred_no = $7, p2_fund = $8, fee_take_type = $9, fund_lack_proc = $10, fund_overdraw = $11, inputed = true, inputer = $12, input_time = CURRENT_TIMESTAMP where uuid = $13", [t40_amt, parseInt(req.body.pay_chnl), req.body.pay_merid, req.body.acct_no, req.body.acct_name, parseInt(req.body.acct_cred_type), req.body.acct_cred_no, req.body.p2_fund, req.body.fee_take_type, req.body.fund_lack_proc, overdraw, req.session.userinfo.uuid, req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 237
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          return res.redirect('/t40_recvr');
        });
      };
    })(this));
  });

  router.get('/check/:id', query_info, function(req, res, next) {
    var client, done, err, record, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.breadcrumbs.push({
      'title': '确认追付'
    });
    if (!res.locals.record.inputed) {
      return next(new Error('该记录尚未追付，不能复核'));
    }
    if (res.locals.record.checked) {
      return next(new Error('该记录已复核，不能重复复核'));
    }
    record = res.locals.record;
    (function(_this) {
      return (function(__iced_k) {
        if (parseFloat(record.t40_amt) !== req.body.pay_amt || parseInt(record.pay_chnl) !== parseInt(record.order_conf[0]) || record.pay_merid !== record.order_conf[1] || record.acct_no !== record.order_conf[2]) {
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/t40_recvr.coffee"
            });
            pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  client = arguments[1];
                  return done = arguments[2];
                };
              })(),
              lineno: 259
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/t40_recvr.coffee"
              });
              client.query("update pay_t40_recvr set inputed = false where uuid = $1", [req.params.id], __iced_deferrals.defer({
                assign_fn: (function() {
                  return function() {
                    err = arguments[0];
                    return result = arguments[1];
                  };
                })(),
                lineno: 265
              }));
              __iced_deferrals._fulfill();
            })(function() {
              if (err) {
                done(client);
                return next(err);
              }
              done();
              return next(new Error('追付信息不一致，不能追付，请检查是否修改了消费代付配置信息，系统已退回此追付记录'));
              return __iced_k();
            });
          });
        } else {
          return __iced_k();
        }
      });
    })(this)((function(_this) {
      return function() {
        return res.render('t40_recvr/check');
      };
    })(this));
  });

  router.post('/check', check_retrefno, function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/t40_recvr.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 275
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/t40_recvr.coffee"
          });
          client.query("update pay_t40_recvr set checked = true, checker = $1, check_time = CURRENT_TIMESTAMP, notes = '已确认追付，等待调度' where uuid = $2", [req.session.userinfo.uuid, req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 285
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          return res.redirect('/t40_recvr');
        });
      };
    })(this));
  });

}).call(this);
